package com.chenfwind.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.chenfwind.common.Constant;
import com.chenfwind.common.ResultVO;
import com.chenfwind.common.StringUtils;
import com.chenfwind.domain.Comment;
import com.chenfwind.domain.Trajectory;
import com.chenfwind.domain.vo.CommentVO;
import com.chenfwind.domain.vo.TrajectoryVO;
import com.chenfwind.service.BookService;
import com.chenfwind.service.CommentService;
import com.chenfwind.service.TrajectoryService;
import com.chenfwind.service.UserService;

@Controller
@RequestMapping("/comment")
public class CommentController {

	@Autowired
	private CommentService commentService;
	
	@Autowired
	private BookService bookService;
	
	@Autowired
	private UserService userService;
	/*
	 * 查看用户的所有通知，比如说，某本书被申请了
	 */
	@ResponseBody
	@RequestMapping(value = "/getMessageByUser" , method = RequestMethod.POST)
	public ResultVO<List<CommentVO>> getMessageByUser(HttpSession session,@RequestParam(value = "userId" ,required = false) String userId){
		ResultVO<List<CommentVO>> vo = ResultVO.create();
		if (userId == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		List<Comment> comment = commentService.findCommentByUserID(userId);
		List<CommentVO> commentVO = new ArrayList<CommentVO>();
		for (Comment com : comment) {
			commentVO.add(Comment2CommentVO(com,null));
		}
		vo.setResult(commentVO);
		vo.reCalcStatus();
		return vo;
	}
	
	public CommentVO Comment2CommentVO(Comment comment ,CommentVO commentVO){
		if (commentVO == null) {
			commentVO = new CommentVO();
			commentVO.setBookName(bookService.getBookNameById(comment.getBookId()));
			commentVO.setUserName(userService.getAccountByUID(comment.getUserId()));
		}
		com.chenfwind.utils.BeanUtils.copyProperties(comment, commentVO);
		return commentVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/changeCommentStatus", method = RequestMethod.POST)
	public ResultVO<String> changeCommentStatus(HttpSession session,@RequestParam(value = "commentId", required = false) String commentId,@RequestParam(value = "status", required = false) String status){
		ResultVO<String> vo = ResultVO.create();
		if (commentId == null) {
			return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
		}
		commentService.changeCommentStatusByCommentId(commentId , status);

		vo.setStatus(Constant.CODE_SUCCESS);;
		return vo;
	}
	
	@ResponseBody
	@RequestMapping(value = "/submitApply", method = RequestMethod.POST)
	public ResultVO<String> submitApply(HttpSession session,
			@RequestParam(value = "commentInfoJson", required = false) String commentInfoJson){
			ResultVO<String> vo = ResultVO.create();
			CommentVO commentInfo = JSON.parseObject(commentInfoJson, CommentVO.class);
			if (commentInfo == null) {
				return ResultVO.create(Constant.CODE_NOPARAM, Constant.CODE_MSG_NOPARAM);
			}
			Comment comment = CommentVO2Comment(commentInfo , null);

			comment.setCreateTime(new Date());
			commentService.save(comment);
			vo.setStatus(Constant.CODE_SUCCESS);;
			return vo;
	}
	
	public Comment CommentVO2Comment(CommentVO commentVO , Comment comment ){
		if (comment == null) {
			comment = new Comment();
			comment.setId(StringUtils.genUUID());
			comment.setStatus(0);
		}
		com.chenfwind.utils.BeanUtils.copyProperties(commentVO, comment);
		return comment;
	}
}
